<?php

namespace App\Admin\Controllers\Order;

use App\Admin\Actions\Grid\Row\SwtOrderRefund\RejectRefundOrder;
use App\Admin\Repositories\SwtOrderRefund;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class SwtOrderRefundController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make( SwtOrderRefund::with(['user','order']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('order.order_sn','订单号');
            $grid->column('user.nickname','用户昵称') -> display( function ($value) {
                if(empty($value)){
                    return '-';
                }
                return $value;
            });
            $grid->column('refund_amount') -> display( function ($value) {
                return $value / 100;
            });
            $grid->column('status')->using([0 => '待审核', 1 => '已通过', 2 => '已驳回'])->badge([
                0 => 'primary',
                1 => 'success',
                2 => 'danger'
            ]);
            $grid->column('notes');
            $grid->column('reject_notes');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid -> actions( function(Grid\Displayers\Actions $actions){
               if($actions->row->status == 0) {
                   $actions->append(new RejectRefundOrder());
               }
            });

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('status', '状态', [
                    0 => '待审核',
                    1 => '已通过',
                    2 => '已驳回'
                ]);
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SwtOrderRefund(), function (Show $show) {
            $show->field('id');
            $show->field('order_id');
            $show->field('user_id');
            $show->field('refund_amount');
            $show->field('status');
            $show->field('notes');
            $show->field('reject_notes');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SwtOrderRefund(), function (Form $form) {
            $form->display('id');
            $form->text('order_id');
            $form->text('user_id');
            $form->text('refund_amount');
            $form->text('status');
            $form->text('notes');
            $form->text('reject_notes');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
